/** 简单锁 */
export class SampleLock {
    callback = null
    status = new Promise((resolve) => { resolve(true) })
    lock = function () {
        this.status = new Promise((resolve) => { this.callback = resolve })
        console.log("locked")
    }
    unlock = function () { this.callback(true) }
}

// 线程锁
let isLock = false;
let lockList = [];
/** 
 * 线程锁
 * @sample
 * let unlock = await lock();
 * 
 * unlock();
 */
export function threadLock() {
    function unlock() {
        let waitFunc = lockList.shift();
        if (waitFunc) {
            waitFunc.resolve(unlock);
        } else {
            isLock = false;
        }
    }
    if (isLock) {
        return new Promise((resolve, reject) => {
            lockList.push({ resolve, reject });
        });
    } else {
        isLock = true;
        return unlock;
    }
}